package com.wg;

import com.wg.enums.HttpCodeEnum;

import java.io.Serializable;


public class R<T> implements Serializable {
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 提示消息
     */
    private String msg;
    /**
     * 响应数据
     */
    private T data;

    public R() {

    }

    public R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> R<T> ok() {
        return new R<T>(HttpCodeEnum.SUCCESS.getCode(), HttpCodeEnum.SUCCESS.getMsg());
    }

    public static <T> R<T> ok(Integer code, String msg) {
        return new R<T>(code, msg);
    }

    public static <T> R<T> ok(T data) {
        R<T> r = new R<T>(HttpCodeEnum.SUCCESS.getCode(), HttpCodeEnum.SUCCESS.getMsg());
        r.data = data;
        return r;
    }

    public static <T> R<T> ok(Integer code, String msg, T data) {
        R<T> r = new R<T>(code, msg);
        r.data = data;
        return r;
    }

    public static <T> R<T> fail() {
        return new R<T>(HttpCodeEnum.SYSTEM_ERROR.getCode(), HttpCodeEnum.SYSTEM_ERROR.getMsg());
    }

    public static <T> R<T> fail(Integer code, String msg) {
        return new R<T>(code, msg);
    }

    public static <T> R<T> fail(T data) {
        R<T> r = new R<T>(HttpCodeEnum.SYSTEM_ERROR.getCode(), HttpCodeEnum.SYSTEM_ERROR.getMsg());
        r.data = data;
        return r;
    }

    public static <T> R<T> fail(Integer code, String msg, T data) {
        R<T> r = new R<T>(code, msg);
        r.data = data;
        return r;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
